#include "commerrno.h"
#include "CBattleActionCtrl.h"
#include "CBattleRules.h"
#include "CBattleInf.h"

/************************************************************************
 描述信息
ACTION_GET_TURN：
    _iA = 行动者roleid

ACTION_ROLE_DEAD:
    _iA = 行动者roleid

************************************************************************/

int CBattleAction::Do()
{
    int iRet = 0;
    CBattleAction stAction = *this;

    switch (_iActionID) 
    {
        case ACTION_GET_TURN:
            iRet = BATTLE_INF->_pstRules->GetTurn(_iA);
            CT_RETURN(iRet);
            break;
        case ACTION_ROLE_DEAD:
            break;
        default:
            return ERROR_UNKNOWN_ACTION_ID;
    }

    return 0;
}

void CBattleAction::DumpActionInfo()
{
    CT_BATTLE(("%-18s = %d", "ActionID", _iActionID));
    CT_BATTLE(("%-18s = %d", "A", _iA));
    CT_BATTLE(("%-18s = %d", "B", _iB));
}

int CBattleActionCtrl::GetActionSize() const
{
    if (_iActionBeginPos > _iActionEndPos)
    {
        return MAX_BATTLE_ACTION_COUNT + _iActionEndPos - _iActionBeginPos;
    }
    else
    {
        return _iActionEndPos - _iActionBeginPos;
    }
}

int CBattleActionCtrl::AddAction(CBattleAction& stBattleAction)
{

    if (IsFull())
    {
        return ERROR_BATTLE_ACTION_FULL;
    }

    _astActions[_iActionEndPos] = stBattleAction;
    _iActionEndPos = (_iActionEndPos + 1) % MAX_BATTLE_ACTION_COUNT;

    return 0;
}

int CBattleActionCtrl::GetAction(CBattleAction& stBattleAction)
{

    if (IsEmpty())
    {
        return ERROR_BATTLE_ACTION_EMPTY;
    }

    stBattleAction = _astActions[_iActionBeginPos];
    _iActionBeginPos = (_iActionBeginPos + 1) % MAX_BATTLE_ACTION_COUNT;

    return 0;
}

int CBattleActionCtrl::PushAction(CBattleAction& stBattleAction)
{

    if (IsFull())
    {
        return ERROR_BATTLE_ACTION_FULL;
    }

    _iActionBeginPos = _iActionBeginPos - 1;
    _iActionBeginPos = _iActionBeginPos < 0 ? MAX_BATTLE_ACTION_COUNT + _iActionBeginPos  : _iActionBeginPos ;
    _astActions[_iActionBeginPos] = stBattleAction;

    return 0;
}

int CBattleActionCtrl::PopAction(CBattleAction& stBattleAction)
{

    if (IsEmpty())
    {
        return ERROR_BATTLE_ACTION_EMPTY;
    }

    _iActionEndPos = _iActionEndPos - 1;
    _iActionEndPos = _iActionEndPos < 0 ? MAX_BATTLE_ACTION_COUNT + _iActionEndPos  : _iActionEndPos ;
    stBattleAction = _astActions[_iActionEndPos];

    return 0;
}

void CBattleActionCtrl::DumpActionCtrlInfo()
{
    CT_BATTLE(("%s", "---- BeginActionCtrl ----"));
    CT_BATTLE(("%-18s = %d", "ActionBeginPos", _iActionBeginPos));
    CT_BATTLE(("%-18s = %d", "ActionEndPos", _iActionEndPos));
    CT_BATTLE(("%s", "-- BattleAction --"));
    for (int i = 0; i < MAX_BATTLE_ACTION_COUNT; ++i)
    {
        CT_BATTLE(("Action Num = %d", i+1));
        _astActions[i].DumpActionInfo();
    }
    CT_BATTLE(("%s", "---- EndActionCtrl ----"));
}

